let base_bidinfo = require("./models/base_bidinfo")(sequelize, DataTypes);

const utils = require("./utils");
const { Op } = require("sequelize");

async function getlist(req,where,order) {
  let r = await base_bidinfo.findAndCountAll({
    offset: ((req.query.pageNum ?? 1) - 1) * (req.query.pageSize ?? 0),
    limit: (req.query.pageSize ?? 100) * 1,
    where,
    order
  });
  return r;
}

app.get("/system/bidinfo/list", async (req, res) => {
  try {
    let where = utils.getwhere(req.query);
    utils.setlike(where,'title',req);
    utils.setlike(where,'keyGuid',req);
    where[Op.and].push({tenderClassNum:['103','105','106']});
    // where[Op.and].push({deletemark:0});
    let r = await getlist(req,where);
    res.send(r);
  } catch (err) {
    console.log(err);
    res.send("err");
  }
});

app.get("/system/bidinfo/:id", async (req, res) => {
  let r = await base_bidinfo.findByPk(req.params.id);
  res.send(r);
});
app.put("/system/bidinfo", async (req, res) => {
  // let r = await consproject.findByPk(req.params.id);
  let r = await base_bidinfo.update(req.body, {
    where: {
      id: req.body.id,
    },
  });
  res.send(r);
});

app.post("/system/bidinfo", async (req, res) => {
  // delete req.body.id;
  req.body.deletemark = 0;
  let r = await base_bidinfo.create(req.body);
  // console.log(r);
  res.send(r);
});

app.get("/system/bidinfo/delete/:id", async (req, res) => {
  let r = await base_bidinfo.update({deletemark:1},{
    where: {
      id: req.params.id,
    },
  });
  res.send(r);
});